package com.huizhonglm.diap.ui.wallet;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.huizhonglm.diap.R;
import com.huizhonglm.diap.api.model.user.IntegralResponse;
import com.huizhonglm.diap.core.utils.DialogUtil;
import com.huizhonglm.diap.core.utils.LogUtil;
import com.huizhonglm.diap.core.utils.ToastUtil;
import com.huizhonglm.diap.core.utils.UserUtil;
import com.huizhonglm.diap.presenters.tab.TabhostMePresenter;
import com.huizhonglm.diap.presenters.wallet.IntegralCashBackPresenter;
import com.huizhonglm.diap.ui.BaseActivity;
import com.huizhonglm.diap.ui.tab.iView.TabhostMeIView;
import com.huizhonglm.diap.ui.wallet.iView.IntegralCashBackIView;

/**
 * 描述当前类的作用
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-03-03 10:53
 */
public class IntegralCashBackActivity extends BaseActivity implements TabhostMeIView, IntegralCashBackIView,
        View.OnClickListener {

    private TextView integralTxt;
    private Button a200Btn;
    private Button a500Btn;
    private Button a1000Btn;
    private Button confirmBtn;

    private String userID;
    private int currIntegral;
    private int applyIntegral;

    private TabhostMePresenter tPresenter;
    private IntegralCashBackPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_integral_cash_back);
        setBackEvent(this, null);
        userID = UserUtil.getUserId();
        tPresenter = new TabhostMePresenter(this);
        mPresenter = new IntegralCashBackPresenter(this);
        assignViews();
    }

    @Override
    protected void onResume() {
        super.onResume();
        showProgressBar();
        tPresenter.refreshIntegral(userID);
    }

    @Override
    protected void assignViews() {
        integralTxt = (TextView) findViewById(R.id.integral_txt);
        a200Btn = (Button) findViewById(R.id.apply_200_btn);
        a500Btn = (Button) findViewById(R.id.apply_500_btn);
        a1000Btn = (Button) findViewById(R.id.apply_1000_btn);
        confirmBtn = (Button) findViewById(R.id.confirm_btn);

        a200Btn.setOnClickListener(this);
        a500Btn.setOnClickListener(this);
        a1000Btn.setOnClickListener(this);
        confirmBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.confirm_btn:
                if (applyIntegral <= 0) {
                    ToastUtil.makeTextShort("请选择要参与的积分数");
                    return;
                }
                DialogUtil.showNormalDialog(this, "确认参与", "您确认使用" + applyIntegral + "积分参与排队吗？", "确定", "取消", new DialogUtil.OnDialogBtnClickListener() {
                    @Override
                    public void onBtnClick(String btnText) {
                        if ("确定".equals(btnText)) {
                            showProgressBar();
                            mPresenter.onParticipate(userID, applyIntegral);
                        }
                    }
                });
                break;
            case R.id.apply_200_btn:
                applyIntegralQueue(R.id.apply_200_btn);
                break;
            case R.id.apply_500_btn:
                applyIntegralQueue(R.id.apply_500_btn);
                break;
            case R.id.apply_1000_btn:
                applyIntegralQueue(R.id.apply_1000_btn);
                break;
        }
    }

    @Override
    public void onRefIntegralSuccess(IntegralResponse resp) {
        hideProgressBar();
        if (resp != null) {
            if (resp.getCurrIntegral() != null) {
                currIntegral = resp.getCurrIntegral();
                setCurrentIntegral(currIntegral);
            } else {
                setCurrentIntegral(0);
            }
        } else {
            setCurrentIntegral(0);
        }
    }

    @Override
    public void onRefIntegralFailure() {
        hideProgressBar();
        setCurrentIntegral(0);
    }

    @Override
    public void onParticipateSuccess() {
        tPresenter.refreshIntegral(userID);
    }

    @Override
    public void onParticipateFailure() {
        hideProgressBar();
        DialogUtil.showConfirmDialog(this, "失败提示", "参与排队失败，请稍后重试。", new DialogUtil.OnDialogBtnClickListener() {
            @Override
            public void onBtnClick(String btnText) {
                showProgressBar();
                tPresenter.refreshIntegral(userID);
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    private void setCurrentIntegral(int integral){
        LogUtil.print("setCurrentIntegral", "integral = " + integral);
        checkedChanged(0);
        if (integral > 0) {
            integralTxt.setText(integral + "");
            confirmBtn.setEnabled(true);
            confirmBtn.setText("确认参与排队返现");
            a200Btn.setEnabled(true);
            a500Btn.setEnabled(true);
            a1000Btn.setEnabled(true);
        } else {
            integralTxt.setText("0");
            confirmBtn.setEnabled(false);
            confirmBtn.setText("当前积分不足");
            a200Btn.setEnabled(false);
            a500Btn.setEnabled(false);
            a1000Btn.setEnabled(false);
        }
    }

    private void applyIntegralQueue(int checkedId){
        if (currIntegral < 200) {
            ToastUtil.makeTextShort("当前积分不足以参与排队");
            checkedChanged(0);
        } else if (currIntegral >= 200 && currIntegral < 500) {
            checkedChanged(R.id.apply_200_btn);
        } else if (currIntegral >= 500 && currIntegral < 1000) {
            if (checkedId == R.id.apply_1000_btn) {
                checkedChanged(R.id.apply_500_btn);
            } else {
                checkedChanged(checkedId);
            }
        } else {
            checkedChanged(checkedId);
        }
    }

    private void checkedChanged(int checkedId){
        switch (checkedId) {
            case R.id.apply_200_btn:
                applyIntegral = 200;
                a200Btn.setTextColor(ContextCompat.getColor(this, R.color.white));
                a200Btn.setBackgroundResource(R.drawable.btn_integral_cash_n);
                a500Btn.setTextColor(ContextCompat.getColor(this, R.color.yahei));
                a500Btn.setBackgroundResource(R.drawable.bg_integral_cash_input);
                a1000Btn.setTextColor(ContextCompat.getColor(this, R.color.yahei));
                a1000Btn.setBackgroundResource(R.drawable.bg_integral_cash_input);
                break;
            case R.id.apply_500_btn:
                applyIntegral = 500;
                a200Btn.setTextColor(ContextCompat.getColor(this, R.color.yahei));
                a200Btn.setBackgroundResource(R.drawable.bg_integral_cash_input);
                a500Btn.setTextColor(ContextCompat.getColor(this, R.color.white));
                a500Btn.setBackgroundResource(R.drawable.btn_integral_cash_n);
                a1000Btn.setTextColor(ContextCompat.getColor(this, R.color.yahei));
                a1000Btn.setBackgroundResource(R.drawable.bg_integral_cash_input);
                break;
            case R.id.apply_1000_btn:
                applyIntegral = 1000;
                a200Btn.setTextColor(ContextCompat.getColor(this, R.color.yahei));
                a200Btn.setBackgroundResource(R.drawable.bg_integral_cash_input);
                a500Btn.setTextColor(ContextCompat.getColor(this, R.color.yahei));
                a500Btn.setBackgroundResource(R.drawable.bg_integral_cash_input);
                a1000Btn.setTextColor(ContextCompat.getColor(this, R.color.white));
                a1000Btn.setBackgroundResource(R.drawable.btn_integral_cash_n);
                break;
            case 0:
                applyIntegral = 0;
                a200Btn.setTextColor(ContextCompat.getColor(this, R.color.yahei));
                a200Btn.setBackgroundResource(R.drawable.bg_integral_cash_input);
                a500Btn.setTextColor(ContextCompat.getColor(this, R.color.yahei));
                a500Btn.setBackgroundResource(R.drawable.bg_integral_cash_input);
                a1000Btn.setTextColor(ContextCompat.getColor(this, R.color.yahei));
                a1000Btn.setBackgroundResource(R.drawable.bg_integral_cash_input);
                break;
        }
        integralTxt.setText(String.valueOf(currIntegral - applyIntegral));
    }
}
